/**
 * Project: saasoa Copyright 2013 Top Digital Technology Co. Ltd. All right reserved.
 */
package com.topdt.jbpm.web.servlet.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.service.AttachmentService;
import com.topdt.jbpm.service.JbpmService;
import com.topdt.jbpm.web.servlet.JbpmServlet;

/**
 * 建立日期 : 2013-7-6 下午2:02:39<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class JbpmBaseAction implements JbpmAction {
  protected final Logger logger = LoggerFactory.getLogger(getClass());
  protected JbpmServlet servlet;
  protected IdentityService idService;
  protected JbpmService jbpmService;
  protected AttachmentService attachmentService;

  public JbpmBaseAction(JbpmServlet servlet) {
    this.servlet = servlet;
    this.idService = servlet.getIdService();
    this.jbpmService = servlet.getJbpmService();
    this.attachmentService = servlet.getAttachmentService();
  }

  public void execute(String command, HttpServletRequest request, HttpServletResponse response) {

    try {
      Method method =
          this.getClass().getDeclaredMethod(command, HttpServletRequest.class,
              HttpServletResponse.class);
      method.invoke(this, request, response);
    } catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void responseJsp(HttpServletRequest request, HttpServletResponse response, String path) {
    try {
      this.servlet.responseJsp(request, response, path);
    } catch (ServletException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void responseJson(HttpServletResponse response, Object jsonObj) {
    try {
      this.servlet.responseJson(response, jsonObj);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void responseJson(HttpServletResponse response, String json) {
    try {
      this.servlet.responseJson(response, json);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void responseError(HttpServletResponse response, String message) {
    try {
      this.servlet.responseError(response, message);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  protected JbpmService getJbpmService() {
    return this.servlet.getJbpmService();
  }

  protected IdentityService getIdService() {
    return this.servlet.getIdService();
  }

  protected void responseRedirec(HttpServletRequest request, HttpServletResponse response,
      String path) throws ServletException, IOException {
    response.sendRedirect(path);
  }

}
